package com.liujit.standard.basis.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.liujit.standard.basis.admin.domain.result.SysRoleMenuAuthResult;
import com.liujit.standard.basis.admin.model.SysRole;
import com.liujit.standard.basis.admin.service.SysRoleMenuService;
import com.liujit.standard.basis.admin.service.SysRoleService;
import com.liujit.standard.basis.admin.service.SysUserRoleService;
import com.liujit.standard.basis.core.util.CoreUtil;
import com.liujit.standard.basis.admin.config.annotation.Auth;
import com.liujit.standard.basis.admin.config.annotation.Log;
import com.liujit.standard.basis.admin.domain.params.RoleMenuParam;
import com.liujit.standard.basis.admin.domain.query.SysRoleQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 系统角色管理
 * @Author LiuJun
 * @Date 2021-03-26 09:00:34
 */
@Api(value = "系统角色管理接口", tags = "系统角色管理接口")
@EnableTransactionManagement
@RestController
@RequestMapping("/admin/sys/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Auth("sysRole-sel")
    @ApiOperation(value = "查询列表", notes = "查询列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysRoleQuery")})
    @GetMapping("/getList")
    public List<SysRole> getList(SysRoleQuery query) {
        CoreUtil.Paging(query, "create_time desc");
        QueryWrapper wrapper = new QueryWrapper();
        if (query.getState() != null) {
            wrapper.eq("state", query.getState());
        }
        if (StrUtil.isNotEmpty(query.getRoleName())) {
            wrapper.like("role_name", query.getRoleName());
        }
        return sysRoleService.list(wrapper);
    }

    @Log
    @Auth("sysRole-add")
    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysRole")})
    @PostMapping("/add")
    public Boolean add(@RequestBody SysRole param) {
        param.setRoleType(Convert.toShort(2));
        return sysRoleService.save(param);
    }

    @Log
    @Auth("sysRole-upd")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @PostMapping("/upd")
    public Boolean upd(@RequestBody SysRole param) {
        return sysRoleService.updateById(param);
    }

    @Log
    @Auth("sysRole-sel")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @GetMapping("/get")
    public SysRole get(Long id) {
        return sysRoleService.getById(id);
    }

    @Log
    @Auth("sysRole-del")
    @ApiOperation(value = "删除/批量删除", notes = "删除/批量删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = ",分割的id")})
    @PostMapping("/del")
    public Boolean del(@RequestBody JSONObject param) {
        String id = param.getString("id");
        return sysRoleService.del(id);
    }

    @Log
    @Auth("sysRole-updState")
    @ApiOperation(value = "修改角色状态", notes = "修改角色状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysRole")})
    @PostMapping("/updState")
    @Transactional(rollbackFor = Exception.class)
    public Boolean updState(@RequestBody SysRole param) {
        return sysRoleService.updateById(param);
    }

    @ApiOperation(value = "查询列表", notes = "查询列表")
    @GetMapping("/getAll")
    public List<SysRole> getAll() {
        PageHelper.orderBy("create_time desc");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("state", 1);
        return sysRoleService.list(wrapper);
    }

    @Auth("sysRole-saveRoleMenu")
    @ApiOperation(value = "根据角色id查询权限", notes = "根据角色id查询权限")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @GetMapping("/getRoleMenu")
    public SysRoleMenuAuthResult getRoleMenu(Long id) {
        return sysRoleService.getRoleMenu(id);
    }

    @Log
    @Auth("sysRole-saveRoleMenu")
    @ApiOperation(value = "保存角色菜单", notes = "保存角色菜单")
    @ApiImplicitParams({@ApiImplicitParam(name = "RoleMenuParam")})
    @PostMapping("/saveRoleMenu")
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveRoleMenu(@RequestBody RoleMenuParam param) {
        return sysRoleService.saveRoleMenu(param);
    }
}
